modimport "scripts/waffles"
modimport "scripts/dynamicmusicfix"

table.insert(PrefabFiles, "epichealth_proxy")

TUNING.EPICHEALTHBAR =
{
	PUSH_MUSIC = true,
	OUT_OF_DATE_OVERRIDE = true,
	NOEPIC = GetModConfigData("noepic", true),
	
	FRAME_PHASES = true,
	DAMAGE_NUMBERS = GetModConfigData("damage", true),
	DAMAGE_RESISTANCE = true,
	WETNESS_METER = true,
	HORIZONTAL_OFFSET = GetModConfigData("offset", true),
	
	BACKGROUND1_COLOUR =				{ 46 / 255, 28 / 255, 30 / 255, 1 },
	BACKGROUND2_COLOUR =				{ 96 / 255, 71 / 255, 74 / 255, 1 },	
	METER_COLOUR =						{ 191 / 255, 36 / 255, 36 / 255, 1 },
	GHOST_TINT =						{ 1, 0.89, 0.8 },
	FRAME_COLOUR =						{ 132 / 255, 102 / 255, 62 / 255, 1 },
	HEAL_COLOUR =						{ 40 / 255, 255 / 255, 80 / 255, 1 },
	DAMAGE_COLOUR =						{ 255 / 255, 80 / 255, 40 / 255, 1 },
	FIRE_COLOUR =						{ 255 / 255, 130 / 255, 62 / 255, 1 },
	ELECTRIC1_TINT =					{ 1.3, 1.15, 1 },
	ELECTRIC2_TINT =					{ 1.1, 1.1, 1.1 },
	ELECTRIC3_TINT =					{ 0.9, 0.9, 0.9 },
		
	THEMES =
	{
		ANTLION =						{ 154 / 255, 80 / 255, 52 / 255 },
		TOADSTOOL =						{ 124 / 255, 72 / 255, 151 / 255 },
		TOADSTOOL_DARK =				{ 0.91, 0.85, 0.24 },
		BEEQUEEN =						{ 0.80, 0.47, 0.13 },
		KLAUS =							{ 191 / 255, 36 / 255, 36 / 255 },
		MINOTAUR =						{ 0.55, 0.52, 0.49 },
		STALKER =						{ 183 / 255, 146 / 255, 200 / 255 },
		STALKER_ATRIUM =				{ 233 / 255, 85 / 255, 107 / 255 },
		STALKER_FOREST =				{ 161 / 255, 186 / 255, 79 / 255 },
		SPIDERQUEEN =					{ 0.93, 0.66, 0.72 },
		LEIF =							{ 48 / 255, 123 / 255, 85 / 255 },
		LEIF_SPARSE =					{ 89 / 255, 115 / 255, 114 / 255 },
		SHADOW_ROOK =					{ 0, 0, 0 },
		SHADOW_KNIGHT =					{ 0, 0, 0 },
		SHADOW_BISHOP =					{ 0, 0, 0 },
		MALBATROSS =					{ 85 / 255, 99 / 255, 164 / 255 },
		CRABKING =						{ 239 / 255, 237 / 255, 140 / 255 },
		LORDFRUITFLY =					{ 151 / 255, 53 / 255, 86 / 255 },
		ALTERGUARDIAN_PHASE1 =			{ 96 / 255, 113 / 255, 137 / 255 },
		ALTERGUARDIAN_PHASE2 =			{ 109 / 255, 199 / 255, 154 / 255 },
		ALTERGUARDIAN_PHASE3 =			{ 156 / 255, 235 / 255, 255 / 255 },
		BOARRIOR =						{ 150 / 255, 46 / 255, 46 / 255 },
		BEETLETAUR =					{ 51 / 255, 153 / 255, 51 / 255 },
		TREEGUARD =						{ 132 / 255, 170 / 255, 74 / 255 },
		TIGERSHARK =					{ 189 / 255, 109 / 255, 24 / 255 },
		KRAKEN =						{ 173 / 255, 130 / 255, 140 / 255 },
		TWISTER  =						{ 99 / 255, 107 / 255, 139 / 255 },
		PUGALISK = 						{ 79 / 255, 78 / 255, 101 / 255 },
		ANTQUEEN =						{ 181 / 255, 122 / 255, 159 / 255 },
		ANCIENT_HULK =					{ 139 / 255, 60 / 255, 40 / 255 },
		ANCIENT_HERALD = 				{ 153 / 255, 51 / 255, 51 / 255 },
		
		DEERCLOPS =
		{
			DEFAULT =					{ 140 / 255, 158 / 255, 176 / 255 },
			DEERCLOPS_YULE =			{ 0.69, 0.23, 0.21 },
		},
		
		BEARGER =
		{
			DEFAULT =					{ 0, 0, 0 },
			BEARGER_YULE = 				{ 0.85, 0.87, 0.69 },
		},
		
		MOOSE =
		{
			DEFAULT =					{ 130 / 255, 123 / 255, 102 / 255 },
			GOOSEMOOSE_YULE_BUILD =		{ 183 / 255, 130 / 255, 66 / 255 },
		},
		
		DRAGONFLY =
		{
			DEFAULT =					{ 90 / 255, 142 / 255, 74 / 255 },
			DRAGONFLY_FIRE_BUILD =		{ 255 / 255, 86 / 255, 18 / 255 },
			DRAGONFLY_YULE_BUILD = 		{ 0.90, 0.71, 0.15 },
			DRAGONFLY_FIRE_YULE_BUILD = { 247 / 255, 146 / 255, 8 / 255 },
		},
	},
	
	PHASES =
	{
		DRAGONFLY =						{ 0.8 },
		TOADSTOOL =						{ 0.7, 0.4 },
		TOADSTOOL_DARK =				{ 0.7, 0.4, 0.2 },
		BEEQUEEN =						{ 0.75, 0.5, 0.25 },
		STALKER =						{ 0.75 },
		STALKER_ATRIUM =				{ 0.625 },
		MALBATROSS =					{ 0.66, 0.33 },
		CRABKING =						{ 0.9, 0.85 },
		TIGERSHARK =					{ 0.1 },
		KRAKEN =						{ 0.75, 0.5, 0.25 },
		ANTQUEEN =						{ 0.75, 0.5, 0.25 },
		ANCIENT_HULK =					{ 0.5, 0.3 },
		
		KLAUS = function(inst)
			if not inst._unchained:value() and inst.epichealth.maxhealth <= TUNING.KLAUS_HEALTH then
				return { 0.5 }
			end
		end,
	},
}

TUNING.EPICHEALTHBAR.THEMES.DEFAULT = TUNING.EPICHEALTHBAR.THEMES

if not TheNet:IsDedicated() then
	table.insert(Assets, Asset("ATLAS", "images/hud/epichealthbar.xml"))
	table.insert(Assets, Asset("ANIM", "anim/epichealthbar.zip"))
	
	if KnownModIndex:IsModEnabledAny("workshop") then
		TUNING.EPICHEALTHBAR.FRAME_COLOUR =			{ 183 / 255, 166 / 255, 166 / 255, 1 }
		TUNING.EPICHEALTHBAR.BACKGROUND1_COLOUR =	{ 38 / 255, 34 / 255, 35 / 255, 1 }
		TUNING.EPICHEALTHBAR.BACKGROUND2_COLOUR =	{ 80 / 255, 71 / 255, 73 / 255, 1 }
	end
	
	local Controls = require "widgets/controls"
	local EpicHealthbar = require "widgets/epichealthbar"
	
	Waffles.Parallel(Controls, "_ctor", function(self)
		self.epichealthbar = self.top_root:AddChild(EpicHealthbar(self.owner))
	end, true)
	
	Waffles.Parallel(Controls, "ShowStatusNumbers", function(self) self.epichealthbar:OnGainFocus() end)
	Waffles.Parallel(Controls, "HideStatusNumbers", function(self) self.epichealthbar:OnLoseFocus() end)
	
	Waffles.Parallel(_G, "Networking_ModOutOfDateAnnouncement", function(mod)
		if TUNING.EPICHEALTHBAR.OUT_OF_DATE_OVERRIDE and mod == KnownModIndex:GetModFancyName(modname) and ThePlayer ~= nil and ThePlayer.HUD ~= nil then		
			if ThePlayer.HUD.eventannouncer.inst:IsValid() then
				for i, v in pairs(ThePlayer.HUD.eventannouncer.active_announcements) do
					if v.shown and v.text ~= nil and v.text.string ~= nil and v.text.string:find(mod) then
						ThePlayer.HUD.eventannouncer:DoShuffleUp(i)
						break
					end
				end
			end
			
			ThePlayer.HUD.controls.epichealthbar:MakeOutdated()
		end
	end, true)
end

--[[!]] if not TheNet:GetIsServer() then return end

local function AddHealthProxy(inst)
	if inst.epichealth == nil then		
		inst.epichealth = inst:SpawnChild("epichealth_proxy")
	end
end

if GetModConfigData("noepic") then
	return AddComponentPostInit("health", function(self, inst) if not inst:HasTag("player") then AddHealthProxy(inst) end end)
end

local function OnAddTag(inst, tag)
	if tag == "epic" then
		AddHealthProxy(inst)
	end
end

AddComponentPostInit("health", function(self, inst)
	if inst:HasTag("epic") then
		AddHealthProxy(inst)
	elseif inst:HasTag("largecreature") or inst:HasTag("hostile") then
		Waffles.Parallel(inst, "AddTag", OnAddTag)
	end
end)